package com.itheima.dao.impl;

import com.itheima.dao.AccountDao;
import com.itheima.domain.entity.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class AccoutDaoImpl implements AccountDao {

    /**
     * 当使用自动按类型注入时，遇到有多个类型匹配的时候，就可以使用此注解来明确注入哪个bean对象。
     * 注意它通常情况下都必须配置@Autowired注解一起使用
     */
    @Autowired
    @Qualifier("jdbcTemplate1")
    private JdbcTemplate jdbcTemplate;

    @Override
    public void save(Account account) {
        jdbcTemplate.update("insert into account(money,name)values(?,?)",account.getMoney(),account.getName());
    }

    @Override
    public void update(Account account) {
        jdbcTemplate.update("update account set name=?,money=? where id=?",account.getName(),account.getMoney(),account.getId());
    }

    @Override
    public void delete(Integer id) {
        jdbcTemplate.update("delete from account where id = ? ",id);
    }

    @Override
    public Account findById(Integer id) {
        return jdbcTemplate.queryForObject("select * from account where id = ?",new BeanPropertyRowMapper<Account>(Account.class),id);
    }

    @Override
    public List<Account> findAll() {
        return  jdbcTemplate.query("select * from account ",new BeanPropertyRowMapper<Account>(Account.class));
    }
}
